<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="账号" prop="accountNumber">
        <el-input
          v-model.trim="form.accountNumber"
          :readonly="formType === 'edit'"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model.trim="form.userName" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" />
      </el-form-item>
      <el-form-item label="用户角色" prop="roleType">
        <el-select
          v-model="form.roleType"
          placeholder="请选择用户角色"
          style="width: 380px"
        >
          <el-option
            v-for="item in roleTypeList"
            :key="item.valueMember"
            :label="item.displayMember"
            :value="item.valueMember"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    RegisterUser,
    GetRoleTypeDropDownList,
    EditUser,
  } from '@/api/sysSetting/users'

  export default {
    name: 'UserEditForm',
    data() {
      return {
        form: {
          id: '',
          accountNumber: '',
          userName: '',
          email: '',
          roleType: 3, //用户角色默认是普通管理员
        },
        rules: {
          accountNumber: [
            { required: true, trigger: 'blur', message: '请输入账号' },
          ],
          userName: [
            { required: true, trigger: 'blur', message: '请输入名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        // 用户角色下拉框数据
        roleTypeList: [],
        // 窗体性质（新增、编辑）
        formType: 'add',
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.formType = 'add'
        } else {
          this.title = '编辑'
          this.formType = 'edit'
          this.form = Object.assign({}, row)
        }
        GetRoleTypeDropDownList().then((res) => {
          this.roleTypeList = res.data
        })
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.formType === 'add') {
              RegisterUser(this.form).then((res) => {
                if (res.data) {
                  this.$baseMessage(res.msg, 'success')
                  this.$emit('fetch-data')
                  this.close()
                } else {
                  this.$baseMessage(res.msg, 'error')
                }
              })
            } else {
              EditUser(this.form).then((res) => {
                if (res.data) {
                  this.$baseMessage(res.msg, 'success')
                  this.$emit('fetch-data')
                  this.close()
                } else {
                  this.$baseMessage(res.msg, 'error')
                }
              })
            }
          }
        })
      },
    },
  }
</script>
